package com.fyf.game.net.serui;

import java.awt.Color;
import java.awt.Graphics2D;
import org.je.image.ImageCanvas;
import com.fyf.game.net.server.MessageServer;
import com.fyf.game.net.server.RunEnvir;
import com.fyf.game.net.server.Transporter;

/**
 * @author fanyifeng 2013-1-9 下午3:05:25
 */
public class ShowCanvas extends ImageCanvas
{
	private MessageServer		ser;
	private static final long	serialVersionUID	= 1L;
	private String				fpsMsg				= "";

	public ShowCanvas(int x, int y, int w, int h, MessageServer ser) {
		super(x, y, w, h);
		this.ser = ser;
	}

	public void draw2D(Graphics2D g) {
		g.setColor(Color.black);
		g.fillRect(0, 0, w(), h());
		g.setColor(Color.white);
		int i = 1;
		RunEnvir env = ser.getRunEnvir();
		String temStr = "端口号: " + env.serverPort + ", 服务状态: " + (env.isServerStart ? "正常启动" : "未启动") + ", " + fpsMsg;
		g.drawString(temStr, 5, 16 * i++);
		g.drawString("读写线程数: " + env.transporterCount(), 5, 16 * i++);
		g.drawString("成功接受连接: " + env.successAcceptCount.get(), 5, 16 * i++);
		g.drawString("连接失败次数: " + env.errorAcceptCount.get(), 5, 16 * i++);
		g.drawString("正确退出次数: " + env.successOutLine.get(), 5, 16 * i++);
		g.drawString("完整的数据包数量: " + env.successReadMsgCount.get(), 5, 16 * i++);
		g.drawString("读取失败次数: " + env.errorReadCount.get(), 5, 16 * i++);
		g.drawString("写入失败次数: " + env.errorWriteCount.get(), 5, 16 * i++);
		g.drawString("各线程注册通道次数: " + transportInfo(), 5, 16 * i++);
		g.drawString("登录人数: " + env.senderSize(), 5, 16 * i++);
		g.drawString("关闭线程数: " + env.closeCount.get(), 5, 16 * i++);

	}

	private String transportInfo() {
		StringBuilder bf = new StringBuilder();
		bf.append("[");
		for (Transporter t : ser.getRunEnvir().tsps) {
			bf.append(t.registerChannelCount.get() + ", ");
		}
		bf.append("]");
		return bf.toString();
	}

	public void onWatch(long t1, long t2, long t3, int fps) {
		fpsMsg = "FPS: " + fps;
	}
}
